<?php

namespace controllers;

use models\SysTokens;
use models\Users;
use system\Route\Route;

class Api extends Common {
    protected        $uid          = '';// 用户ID
    protected        $userType     = '';// 用户类型
    protected        $verifyType   = 1; // 默认校验 后台用户
    protected        $requestValid = false; // 请求是否有效
    protected        $token        = ''; // token
    protected static $pageSize     = '';

    /**
     * 初始化方法
     * Api constructor.
     */
    public function __construct() {
        parent::__construct();
        // setConfig
        self::setConfig();
        $this->verifyToken();
    }

    /**
     * 验证token
     * @return string
     */
    protected function verifyToken() {
        $token       = $_SERVER['HTTP_ACCESS_TOKEN'];
        $this->token = $token;
        $routes      = Route::getRoute();
        $inUrl       = [
            '/admin/user/login',
            '/admin/verify',
            '/api/getInfo',
            '/api/bind',
            '/api/userinfo',
            '/api/getVerifyCode',
            '/admin/test' ,
        ];
        if ($routes == '/admin/getNavs' || $routes == '/api/group') {
            $token = values('get.access_token');
        }
        $this->requestValid = true;
        if (!$token && !in_array($routes, $inUrl)) {
            $this->needLogin();
        }
        if ($token) {
            $model = new SysTokens();
            $map   = [
                'token'       => $token,
                'expire_time' => ['gt', time()],
            ];
            $find  = $model->getFind($map, 'uid,type,expire_time');
            if (!$find) {
                $this->needLogin();
            }
            if ($find['type'] == 2) {
                // 检验用户管理员
                $userFind = (new Users())->getFind($find['uid'], 'id,phone,join_id');
                if ((!$userFind || !$userFind['phone'] || !$userFind['join_id']) && !in_array($routes, $inUrl)) {
                    $this->needLogin();
                }
            }
            $this->uid      = $find['uid'];
            $this->userType = $find['type'];
            if (!$this->checkVerify()) {
                exit(error('您的权限不足'));
            }
            // 小于5分钟的话，就更新
            if ($find['expire_time'] - time() < 300) {
                // 更新用户的token过期时间
                $model->updateToken($this->uid, $this->userType, $token);
            }
        }
    }

    /**
     * 检测权限
     * @return bool
     */
    protected function checkVerify() {
        if ($this->verifyType == $this->userType) {
            return true;
        }

        return false;
    }

    /**
     * 需要登录
     */
    protected function needLogin() {
        exit(error('need login', 1001));
    }

    /**
     * 获取分页信息
     * @return array
     */
    protected static function getPageInfo() {
        $page  = values('get.page');
        $limit = values('get.limit');
        if (!$limit) {
            $limit = static::$pageSize;
        }
        if (!$page) {
            $page = 1;
        }

        return [$page, $limit];
    }

    /**
     * 退出方法
     * @return bool
     */
    protected function logout() {
        $token = $this->token;
        $model = new SysTokens();
        $model->unsetTokens($token);

        return true;
    }

    /**
     * 请求是否有效
     * @return bool
     */
    protected function isValid() {
        if ($this->requestValid && $this->uid) {
            return true;
        }

        return false;
    }
}
